package com.yazi.mxz.person.controller.firm;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myframework.smhj.hibernate.SearchingFactory;
import com.myframework.smhj.hibernate.Sorting;
import com.myframework.smhj.jackson.result.ListResult;
import com.myframework.smhj.jackson.result.StatusResult;
import com.yazi.mxz.common.entity.beans.MemberPromote;
import com.yazi.mxz.common.entity.enums.MemberPromoteType;
import com.yazi.mxz.common.service.MemberPromoteService;
import com.yazi.mxz.person.jackson.filter.MemberPromoteFilter;

@Controller("firm-memberPromoteController")
@RequestMapping("/firm/memberPromote")
public class MemberPromoteController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(MemberPromoteController.class);

	@Resource
	MemberPromoteService memberPromoteService;

	@RequestMapping
	public String index(Model model) {
		debug(log, "充值促销");
		try {
			
		} catch (Exception e) {
			error(log, e, "充值促销查询异常");
			return FORWARD_TIP;
		}
		model.addAttribute("memPromote", getCompany(true).getMemPromote());
		return "firm/memberPromote";
	}
	
	@RequestMapping("/list/json")
	@ResponseBody
	public ListResult<?> listJson(MemberPromote memberPromote) {
		debug(log, "获取充值促销列表");
		ListResult<MemberPromote> jsonResult = new ListResult<MemberPromote>();
		jsonResult.setFilter(MemberPromoteFilter.get());
		try {
			memberPromote.setCompany(getCompany());
			
			// 指定排序
			Sorting sorting = SearchingFactory.createSorting("prepaid");
			
			List<MemberPromote> list = memberPromoteService.list(memberPromote, sorting);
			jsonResult.setResult(list);
			jsonResult.setOk();
		} catch (Exception e) {
			error(log, e, "获取充值促销列表异常");
			jsonResult.setErrorMessage("操作失败,请联系管理员!");
		}
		return jsonResult;
	}
	
	@RequestMapping("/addDo/json")
	@ResponseBody
	public StatusResult addDoJson(MemberPromoteType type, InnerObject obj) {
		debug(log, "保存充值促销");
		StatusResult jsonResult = new StatusResult();
		try {
			memberPromoteService.addByCompany(getCompany(), type, obj.getMps());
			jsonResult.setOk();
		} catch (Exception e) {
			error(log, e, "保存充值促销异常");
			jsonResult.setErrorMessage("保存失败,请联系管理员!");
		}
		return jsonResult;
	}
	
	public static class InnerObject {
		private List<MemberPromote> mps;

		public List<MemberPromote> getMps() {
			return mps;
		}

		public void setMps(List<MemberPromote> mps) {
			this.mps = mps;
		}
	}

}
